package org.newdawn.fizzy.examples;

import org.newdawn.fizzy.Body;
import org.newdawn.fizzy.Circle;
import org.newdawn.fizzy.Polygon;
import org.newdawn.fizzy.Rectangle;
import org.newdawn.fizzy.World;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Universe {

	public World world;
	private Renderer renderer;

	
	public Universe(int x, int y) throws SlickException {
		world = new World(-x,-y,x,y ,10, World.DEFAULT_ITERATIONS);
		initWorld(x,y);
		renderer = new Renderer();
	}
	
	public void initWorld(int x, int y) {
		reset();
		
		for (int i=0;i<5;i++) {
			Body body = new Body(new Rectangle(30.0f , 30.0f , 200.0f , 0.015f, 1f), 300-(6*i), 50*i, false);
			world.add(body);
		}
//		
		for (int i=0;i<4;i++) {
			Body body = new Body(new Circle(10.0f , 100.0f , 0.001f , 100.0f), 350+(10*i), 270-(25*i), false);
			world.add(body);
		}
		Polygon poly = new Polygon();
		float[] d = {0,702, 0,700, 50,70, 60,80};// 90,20, 100,50, 150,80, 220,90, 350, 400, 1000,700, 1000,702};
		for(int i =0; i<d.length-1;i+=2)
		{
			poly.addPoint(d[i], d[i+1]);
		}
		Body body = new Body(poly, 350, 0, false);
		world.add(body);
		
		body = new Body(new Rectangle(30.0f , 30.0f , 200.0f , 0.015f, 1f), 350, 400, true);
		world.add(body);
		
		Body floor = new Body(new Rectangle(820.0f, 10.0f, 200f, 0.015f, 1f), 350, 455.0f, true);
		world.add(floor);
		
		Body wall = new Body(new Rectangle(100.0f, 50.0f), 0, 430.0f, true);
		world.add(wall);
		Body wall2 = new Body(new Rectangle(100.0f, 50.0f), 860, 430.0f, true);
		world.add(wall2);
	}
	
	public void render(Graphics g) throws SlickException {
		for (int i=0;i<world.getBodyCount();i++) {
			renderer.drawBody(g, world.getBody(i));
		}
	}
	
	private void reset() {
		System.out.println("will remove "+world.getBodyCount()+" bodies.");
		for (int i=0;i<world.getBodyCount();i++) {
			world.remove(world.getBody(i));
		}
	}
	
	
}
